<?php
/**
 * This is LilBlogs helper.
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil.lil_blogs
 * @subpackage    lil.lil_blogs
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * LilBlogsHelper class
 *
 * @uses          Helper
 * @package       lil_blogs
 * @subpackage    lil_blogs.views.helpers
 */
class LilBlogsHelper extends AppHelper {
/**
 * helpers property
 *
 * @var array
 * @access public
 */
	var $helpers = array('Html', 'Text', 'Lil.Lil');
/**
 * permaLink method
 *
 * @param string $blog_name
 * @param mixed $post
 * @param array $options
 * @access public
 * @return void
 */
	function permalink($post, $options = array()) {
		$default_options = array(
			'caption' => $post['Post']['title']
		);
		$options = array_merge($default_options, (array)$options);
		
		return $this->Html->link(
			$options['caption'],
			array(
				'admin'      => false,
				'plugin'     => 'lil_blogs',
				'controller' => 'posts',
				'action'     => 'view',
				'blog'       => $post['Blog'][Configure::read('Lil.areaSlugField')],
				'post'       => $post['Post']['slug']
			),
			array(
				'rel' => 'bookmark'
			)
		);
	}
/**
 * function exceprt
 * 
 * Extracts excerpt from text
 *
 * @param string $body
 * @param array $options
 * @return string
 * @access public
 * @static
 */
	function excerpt($post = null, $options = array()) {
		$default_options = array(
			'excerpt_length'    => null,
			'excerpt_delimiter' => Configure::read('LilBlogs.excerptDelimiter'),
			
			'more' =>
				sprintf(
					'<div class="more">%s</div>',
					$this->Html->link(
						__d('lil_blogs', 'Read more', true),
						array(
							'plugin'     => 'lil_blogs',
							'controller' => 'posts',
							'action'     => 'view',
							'blog'       => $post['Blog'][Configure::read('Lil.areaSlugField')],
							'post'       => $post['Post']['slug']
						)
					)
				)
		);
		
		$options = array_merge($default_options, $options);
		extract($options);
		
		$body = $this->Lil->autop($post['Post']['body']);

		if (!empty($excerpt_length) || $excerpt_length = Configure::read('LilBlogs.excerptLength')) {
			$ret = $this->Text->truncate(
				$body, 
				$excerpt_length,
				array(
					'ending' => '...',
					'exact'  => false,
					'html'   => true
				)
			);
		} else if (!empty($excerpt_delimiter) && stripos($body, $excerpt_delimiter) !== false) {
			$ret = substr($body, 0, stripos($body, $excerpt_delimiter));
		}
		
		if (strlen($ret) != strlen($body) && !empty($more)) {
			$ret .=	PHP_EOL . $more;
		}
		
		return $ret;
	}
}
?>